import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/pages/goods/binding/goods_detail_binding.dart';
import 'package:cbec_app/pages/goods/view/goods_detail_page.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class GoodsItem extends StatefulWidget {
  ///商品信息
  final Map<String, dynamic>? goodsInfo;

  ///容器宽度
  var boxWidth = 157.w;

  ///容器高度(不传时将自动计算容器高度)
  final double? boxHeight;

  ///容器背景
  final Color? boxColor;

  ///商品类型 0-普通商品 1-特价商品 2-拼团商品 3-秒杀商品 4-砍价商品 5-预售商品
  final int goodsType;

  ///商品item(用于列表展示相关)
  GoodsItem({super.key, this.boxHeight, this.goodsInfo, this.goodsType = 0, this.boxColor = Colors.white});

  @override
  State<StatefulWidget> createState() => _GoodsItemState();
}

class _GoodsItemState extends State<GoodsItem> {
  @override
  Widget build(BuildContext context) {
    return BaseInteractive(
      childWidget: Container(
        decoration: border_radis(widget.boxColor, radiusValue: 10),
        width: 176.w,
        height: widget.boxHeight,
        padding: EdgeInsets.only(bottom: 8),
        clipBehavior: Clip.hardEdge,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //商品图片
            BaseImage(url: widget.goodsInfo?['mainImage'], width: 176.w, height: 187.h),
            //商品信息
            Container(
              width: widget.boxWidth,
              margin: EdgeInsets.only(top: 0.h, left: 10.w, right: 10.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  //商品类型和名称
                  Text.rich(
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      TextSpan(children: [
                        WidgetSpan(
                            alignment: PlaceholderAlignment.middle, // 垂直居中对齐
                            child: goodsTypeTag(getGoodsTypeName(widget.goodsInfo?['commodityType']), margin: const EdgeInsets.only(right: 5))),
                        _goodsName(),
                      ])),

                  SizedBox(
                    child: Wrap(
                      runSpacing: 5.w,
                      children: [
                        for (var text in widget.goodsInfo?['discountLabel'])
                          BorderedText(
                            text: text,
                            padding: const EdgeInsets.only(left: 2, right: 2, top: 2, bottom: 2),
                            borderColor: ThemeConfig.themeColor,
                            textStyle: const TextStyle(fontSize: 11, color: ThemeConfig.themeColor),
                          ),
                      ],
                    ),
                  ),

                  ///商品价格
                  Container(
                    margin: EdgeInsets.only(top: 10.h),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [goodsSalesPrice(widget.goodsInfo!['price'].toString()), goodsOriginPrice(widget.goodsInfo!['priceMarket'].toString())],
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
      onClick: () {
        final id = widget.goodsInfo?['commodityId'];
        Get.to(
          () => GoodsDetailPage(flag: id), // 使用函数懒加载页面实例
          routeName: '${Routes.goodsDetail}?unique=${DateTime.now().millisecondsSinceEpoch}',
          arguments: {"id": id}, // 传递其它参数
          binding: GoodsDetailBinding(flag: id), // 将 tag 参数传递给绑定类
        );
      },
    );
  }

  ///商品名称
  TextSpan _goodsName() {
    return TextSpan(text: widget.goodsInfo?['commodityName'], style: setTextStyle(14, FontWeight.w400, ThemeConfig.mainTextColor));
  }
}
